var com = {};

// 模块链接
com.murl = function (d,querystring){
	var app = getApp();
	return app.util.url('entry/wxapp/'+d,querystring);
}

com.http = function(doo,method,data,cachetime,loading,success,fail,complete){
	var app = getApp();
	var cachetime = app.siteInfo.siteroot == 'http://127.0.0.5/app/index.php' ? 0 : cachetime;
    app.util.request({
        url: app.com.murl(doo),
        method : method,
        cachetime: cachetime ? cachetime : 0,
        data : data,
        showLoading : loading ? true : false,
        success: function (res) {
            if( success ) success( res );
      	},
      	complete : function(res){
      		if( complete ) complete( res );
      	},
        fail : function(res){
            if( fail ) fail( res );
        }
    });
}

com.auth = function( cb ){
    var self = this;

    self.getUserInfo(
        function(res){
            if( cb ) cb(res);
        },
        function(res){
            self.toAuth(cb);
        }
    );
}

com.toAuth = function( cb ){
    var self = this;
    wx.showModal({
        title: '提示',
        content : '请允许使用您的用户信息',
        showCancel : false,
        complete : function() {
            wx.openSetting({
                success: function(resa){
                    
                    if( !resa.authSetting['scope.userInfo'] ) {
                        self.toAuth( cb );
                    }else{
                        self.auth( cb );
                    }
                }
            })
        }
    });
}

/*
* 获取用户信息
*/
com.getUserInfo = function (cb,fb) {
    var app = getApp();
    var login = function() {
        var app = getApp();
        var userInfo = {
            'sessionid': '',
            'wxInfo': '',
            'memberInfo': '',
        };
        wx.login({
            success: function (res) {
                app.util.request({
                    url: 'auth/session/openid',
                    data: { code: res.code },
                    cachetime: 0,
                    success: function (session) {
                        
                        if (!session.data.errno) {
                            userInfo.sessionid = session.data.data.sessionid
                            wx.setStorageSync('userInfo', userInfo);
                            wx.getUserInfo({
                                success: function (wxInfo) {
                                    userInfo.wxInfo = wxInfo.userInfo
                                    wx.setStorageSync('userInfo', userInfo);
                                    app.util.request({
                                        url: 'auth/session/userinfo',
                                        data: {
                                            signature: wxInfo.signature,
                                            rawData: wxInfo.rawData,
                                            iv: wxInfo.iv,
                                            encryptedData: wxInfo.encryptedData
                                        },
                                        method: 'POST',
                                        header: {
                                            'content-type': 'application/x-www-form-urlencoded'
                                        },
                                        cachetime: 0,
                                        success: function (res) {
                                            if (!res.data.errno) {
                                                userInfo.memberInfo = res.data.data;
                                                wx.setStorageSync('userInfo', userInfo);
                                            }

                                            typeof cb == "function" && cb(userInfo);
                                        }
                                    });
                                },
                                fail: function (e) {
                                    typeof fb == "function" && fb(e);
                                },
                                complete: function () {
                                }
                            })
                        }
                    }
                });
            },
            fail: function () {
                
                wx.showModal({
                    title: '获取信息失败',
                    content: '请允许授权以便为您提供给服务',
                    success: function (res) {
                        if (res.confirm) {
                            app.util.getUserInfo();
                        }
                    }
                })
            }
        });
    };

    var uapp = wx.getStorageSync('userInfo');

    if (uapp.sessionid) {
        
        login();
        wx.checkSession({
            success: function(){

                //typeof cb == "function" && cb(app);
            },
            fail: function(){
                
                uapp.sessionid = '';
                //console.log('relogin');
                wx.removeStorageSync('userInfo');
                login();
            }
        })
    } else {
        //调用登录接口
        login();
    }
}



com.getPage = function(that,data,cachetime,succall,comcall,fail){
    if( data.doing || data.isend ) return false;
    data.doing = true;

    that.setData({
        'page.nodataf' : 0,
        'page.waitf' : 1,
    });

    this.http( data.doo ? data.doo : 'pagelist','GET',data.pdata,cachetime,false,function(res){
        
        if( res.data.data.length > 0 ){
            data.pdata.page ++;
        }else{
            data.isend = true;
            that.setData({
                'page.nodataf' : 1
            });
        }
        if( succall ) succall(res);

    },function(res){

        if( fail ) fail(res);

    },function(res){
        if( comcall ) comcall(res);
        data.doing = false;
        that.setData({
            'page.waitf' : 0
        });
    });
}


com.location = function( e ){
    wx.openLocation({
      latitude:  parseFloat(e.currentTarget.dataset.lat),
      longitude: parseFloat(e.currentTarget.dataset.lng),
      scale: 13
    });
}

com.otherapp = function( e ){
        
    wx.navigateToMiniProgram({
        appId: e.currentTarget.dataset.appid,
        path: e.currentTarget.dataset.appurl,
        success : function(){
            console.log( 'tosuc' );
        },
        fail : function(m){
            app.util.message('打开页面失败','','error');
        }
    });
}

com.navigateto = function( e ){
    if( e.currentTarget.dataset.url ) {
        wx.navigateTo({
          url: e.currentTarget.dataset.url
        })
    }
}

com.redirectto = function( e ){

    if( e.currentTarget.dataset.url ) {
        wx.redirectTo({
          url: e.currentTarget.dataset.url
        })
    }
}

com.callphone = function(e){
    
    var tel = e.currentTarget.dataset.tel;
    if( tel ) {
        wx.makePhoneCall({
          phoneNumber: tel
        });
    }
}

com.toweburl = function( e ){
    var url = e.currentTarget.dataset.weburl;
    
    if( url ) {
        var weburl = encodeURIComponent( url );
        wx.navigateTo({
          url: '/zofui_sitetemp/pages/webview/webview?url='+weburl,
        });
    }
}

com.isArr = function(arr){
    return Object.prototype.toString.call(arr) === '[object Array]'; 
}



module.exports = com;